# Copyright (c) 2009 Olexandr Melnyk <me@omelnyk.net>
# See COPYING for details.

import re
import urllib2

class TydaDictionary:
    # Constructor
    def __init__(self):
        self.short_name = "tyda"
        self._word_url = 'http://tyda.se/search?w=%s'

    # Returns list of supported language pairs
    def supported_pairs(self):
        return (("en", "sv"), ("sv", "en"))
    
    # Translates word from source_lang to target_lang
    def translate(self, word, source_lang, target_lang):
        html    = urllib2.urlopen(self._word_url % word).read()
        pattern = '(<a id="tyda_transR\d" href="[^<]+">)([^<]+)(<\/a>)'
        results = re.findall(pattern, html)
        return list(set([res[1] for res in results]))
